/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2012 Gary Paluk, http://www.plugin.io
 *
 * Permission is hereby granted, free of charge, to any person
 * obtaining a copy of this software and associated documentation
 * files (the "Software"), to deal in the Software without
 * restriction, including without limitation the rights to use,
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following
 * conditions:
 *
 * The above copyright notice and this permission notice shall be
 * included in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * http://www.opensource.org/licenses/mit-license.php
 */

package io.plugin.collections.core 
{
	import io.plugin.collections.interfaces.ICollection;
	import io.plugin.collections.interfaces.IIterator;
	import io.plugin.core.errors.UnsupportedOperationError;
	import io.plugin.core.interfaces.IEquatable;
	import io.plugin.collections.ns.plugin_collections;
	
	/**
	 * ...
	 * @author Gary Paluk - http://www.plugin.io
	 */
	public class AbstractCollection implements ICollection 
	{
		
		use namespace plugin_collections;
		
		public function AbstractCollection( c: ICollection = null ) 
		{
			if ( c )
			{
				addAll( c );
			}
		}
		
		public function add( o: Object ): Boolean
		{
			throw new UnsupportedOperationError();
		}
		
		public function addAll( c: ICollection ): Boolean
		{
			var iter: IIterator = c.iterator();
			var modified: Boolean = false;
			while ( iter.hasNext() )
			{
				if ( add( iter.next() ) )
				{
					modified = true;
				}
			}
			return modified;
		}
		
		public function clear(): void
		{
			var iter: IIterator = iterator();
			while ( iter.hasNext() )
			{
				iter.next();
				iter.remove();
			}
		}
		
		public function equals( o: Object ): Boolean
		{
			throw new UnsupportedOperationError( );
		}
		
		public function contains( o: Object ): Boolean
		{
			var iter: IIterator = iterator();
			while ( iter.hasNext() )
			{
				if ( equals( iter.next() ) )
				{
					return true;
				}
			}
			return false;
		}
		
		public function containsAll( c: ICollection ): Boolean
		{
			var iter: IIterator = c.iterator();
			while ( iter.hasNext() )
			{
				if ( !contains( iter.next() ) )
				{
					return false;
				}
			}
			return true;
		}
		
		public function isEmpty(): Boolean
		{
			return (size() == 0);
		}
		
		public function iterator(): IIterator
		{
			throw new UnsupportedOperationError( );
		}
		
		public function remove( o: Object ): Boolean
		{
			if ( !(o is IEquatable ) )
			{
				return false;
			}
			
			var iter: IIterator = iterator();
			while ( iter.hasNext() )
			{
				if ( o.equals( iter.next() ) )
				{
					return true;
				}
			}
			return false;
		}
		
		public function removeAll( c: ICollection ): Boolean
		{
			var iter: IIterator = c.iterator();
			var modified: Boolean = false;
			while ( iter.hasNext() )
			{
				if ( remove( iter.next() ) )
				{
					modified = true;
				}
			}
			return modified;
		}
		
		public function retainAll( c: ICollection ): Boolean
		{
			var iter: IIterator = iterator();
			var modified: Boolean = false;
			while ( iter.hasNext() )
			{
				if ( !contains( iter.next() ) )
				{
					iter.remove();
					modified = true;
				}
			}
			return modified;
		}
		
		public function size(): uint
		{
			throw new UnsupportedOperationError( );
		}
		
		public function toArray(): Array
		{
			var iter: IIterator = iterator();
			var array: Array = [];
			var i:int;
			
			while( iter.hasNext() )
			{
				array[ i++ ] = iter.next();
			}
			return array; 
		}
		
	}

}